//
//  XXTableView.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/4/6.
//

import UIKit

class XXTableView: UITableView,UITableViewDelegate,UITableViewDataSource {

    var arrData:Array = Array<Any>.init()
    var pageNum = 1
    var pageSize = 20
    var isOpenHeaderRefresh:Bool = false
    var isOpenFooterRefresh:Bool = false
    var selectTimeInterval = 0.5
    var isCanSelect = true
    
    var numberOfRows = 0
    var sections = 1
    var heightForFooter:CGFloat = 0.0
    var heightForHeader:CGFloat = 0.0
    
    var moreData:Bool = false
//    var loadTyep:XSLoadType = .refresh


    func configTableView() {
        pageNum = 1
        pageSize = 20
        
    }
    // MARK: - 上下 拉 刷新/加载
    func openHeaderRefresh() {
        if self.isOpenHeaderRefresh == false {
            self.isOpenHeaderRefresh = true
            if self.isOpenHeaderRefresh {
                
//                let header = MJRefreshStateHeader.init(refreshingBlock: {
//                    self.headAction()
//                })
//                header?.lastUpdatedTimeLabel?.textColor = .hex666
//                header?.stateLabel?.textColor = .hex666
//                self.tableView.mj_header = header;
            }
        }
    }
    func openFooterRefresh() {
        if self.isOpenFooterRefresh == false {
            self.isOpenFooterRefresh = true
            if self.isOpenFooterRefresh {
                
//                let footer = MJRefreshAutoStateFooter.init(refreshingBlock: {
//                    self.footAction()
//                })
//                footer?.setTitle("无更多数据", for: MJRefreshState.noMoreData)
//                footer?.stateLabel?.textColor = .hex666
//                self.tableView.mj_footer = footer
            }
        }
        
    }
    func beginHeaderRefresh() {
//        self.tableView.mj_header?.beginRefreshing()
    }
    func beginFooterRefresh() {
//        self.tableView.mj_footer?.beginRefreshing()
    }
    func endHeaderRefresh() {
//        self.tableView.mj_header?.endRefreshing()
    }
    func endFooterRefresh() {
//        self.tableView.mj_footer?.endRefreshing()
    }
    func endFooterNoMoreData() {
//        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
    }
    func headAction(){
//        self.getList(type: .refresh)
    }
    func footAction(){
//        self.getList(type: .more)
    }
//    func getList(type:XSLoadType) {
//        loadTyep = type
//        switch type {
//        case .refresh:
//            self.pageNum = 1
//            self.arrData.removeAll()
//            break
//        case .more: break
//        }
//    }
    // 判断是否有更多数据
    func appendData(_ arr:Array<Any>? ,_ showEmptyView:Bool = false) {
        
        self.arrData += arr!
        self.reloadData()
        self.endHeaderRefresh()
        if arr!.count == pageSize{
            self.endFooterRefresh()
            self.pageNum += 1
            self.moreData = true
        }
        else{
            self.endFooterNoMoreData()
            self.moreData = false
        }
        
//        if loadTyep == .refresh {
//            if showEmptyView{
//                if arr?.count == 0 {
//                    emptyView.isHidden = false
//                    tableView.isScrollEnabled = false
//                }else{
//                    emptyView.isHidden = true
//                    tableView.isScrollEnabled = true
//                }
//
//            }
//        }
    }
    func reloadRows(arr:Array<IndexPath>) {
        self.reloadRows(at: arr, with: .none)
    }
    func reloadSection(arr:Array<Int>) {
        for i in arr {
            self.reloadSections(IndexSet(integer: i), with: .none)
        }
    }
    // MARK: - 代理相关
    func selectRow(indexPath: IndexPath) {
        print("点击cell_子类重新此方法")
    }
    func cellForRow(tableView: UITableView ,indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "default_cell")
        cell.selectionStyle = .none
        return cell
    }
    func contentViewForRow(indexPath: IndexPath, contentView:UIView ,cell:UITableViewCell)  {
        
    }
    func numberOfRowsInSection(section: Int) -> Int {
        return numberOfRows
    }
    func heightForRow(indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func heightForHeaderInSection(section: Int) -> CGFloat {
        return heightForHeader
    }
    func heightForFooterInSection(section: Int) -> CGFloat {
        return heightForFooter
    }
    func headerViewForSection(section: Int, contentView:UIView)  {
        
    }
    func footerViewForSection(section: Int, contentView:UIView)  {
        
    }
    // MARK: - tableView
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if isCanSelect == true {
            
            selectRow(indexPath: indexPath)
            isCanSelect = false
            Timer.scheduledTimer(withTimeInterval: selectTimeInterval, repeats: false) {  [weak self] (Timer) in
                Timer.invalidate()
                self?.isCanSelect = true
            }
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return numberOfSections
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfRowsInSection(section: section)
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = cellForRow(tableView: tableView,indexPath: indexPath)
        cell.selectionStyle = .none
        contentViewForRow(indexPath: indexPath, contentView: cell.contentView ,cell: cell)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return heightForHeaderInSection(section: section)
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return heightForFooterInSection(section: section)
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = XXView.init(frame: kFrame(0, 0, CGFloat(kScreenW), 40), color: .background)
        self.headerViewForSection(section: section, contentView: headerView)
        return headerView
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = XXView.init(frame: kFrame(0, 0, kScreenW, 40), color: .background)
        self.footerViewForSection(section: section, contentView: footerView)
        return footerView
    }
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return self.heightForRow(indexPath: indexPath)
//    }

    static func tableView() -> XXTableView{
        let tableView = XXTableView.init(frame: .zero, style: .plain)
        tableView.delegate = tableView
        tableView.dataSource = tableView
        tableView.showsVerticalScrollIndicator = false
        tableView.tableFooterView = UIView.init(frame:.zero)
        return tableView
    }
}
